App.controller('stockPurchaseController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {

    $scope.stockSelected = '';
    $scope.rowId = 1;

    $scope.grid = {
        add : function() {
            $scope.stockPurchase = {};
            $scope.modal.setTitle('新增').show();
        },
        update: function(row) {
            $scope.stockPurchase = row;
            Page.ajaxPost('stockPurchaseDetail/queryByListId.do', {listId : row.listId}, function(resp) {
                $scope.stockPurchaseDetail = resp.data;
                $($scope.stockPurchaseDetail).each(function(index, row){
                    row.rowId = $scope.rowId ++;
                });
                $scope.modal.setTitle('编辑【单号：' + row.listId + '】').show();
            });
        },
        save : function() {
            if(!$scope.validateForm.$valid) {
                $scope.validateForm.doValidate();
            } else {
                $($scope.stockPurchaseDetail).each(function (i, item) {
                    item.typeId = item.typeId.typeId;
                });
                $scope.stockPurchase.detail = $scope.stockPurchaseDetail;

                $scope.stockPurchase.deptName = $scope.stockPurchase.deptId.deptName;
                $scope.stockPurchase.deptId = $scope.stockPurchase.deptId.deptId;

                Page.ajaxPost($scope.stockPurchase.listId ? 'stockPurchase/update.do' : 'stockPurchase/add.do', $scope.stockPurchase, function() {
                    $scope.grid.query();
                    $scope.grid.cancel();
                    Page.notice("保存成功！");
                });
            }
        },
        column_defs: [{
            target : 8,
            buttons : [
                {
                    name: '编辑',
                    class: 'btn-warning btm-sm rounded',
                    icon: 'fa-edit',
                    event: 'update'
                },
                {
                    name: '删除',
                    class: 'btn-danger rounded',
                    icon: 'fa-close',
                    event: function(row) {
                        Page.confirm("确定要删除吗？", function() {
                            Page.ajaxPost('stockPurchase/delete.do', row, function() {
                                $scope.grid.query();
                            });
                        });
                    }
                }
            ]
        }]
    };

    $scope.gridStock = {
        column_defs: [{
            target : 0,
            buttons : [
                {
                    name: '选择',
                    class: 'btn-warning btm-sm rounded',
                    icon: 'fa-check',
                    event: function(row) {
                        angular.element($scope.currentRow).scope().item.stockId = row.stockId;
                        angular.element($scope.currentRow).scope().item.typeId = row.typeId;
                        angular.element($scope.currentRow).scope().item.typeName = row.typeName;
                        angular.element($scope.currentRow).scope().item.spec = row.spec;
                        angular.element($scope.currentRow).scope().item.brand = row.brand;
                        angular.element($scope.currentRow).scope().item.unit = row.unit;
                        angular.element($scope.currentRow).scope().item.stockName = row.stockName;
                        $scope.modalStock.hide();
                    }
                }
            ]
        }]
    };

    /*自定义查询*/
    $scope.query = function () {
        $scope.grid.query();
    };

    /*自定义重置*/
    $scope.reset = function () {
        $scope.grid.reset();
    };

    /*模糊查询回显*/
    $scope.stockRet = function (index) {
        rowId = index + 1;
        angular.element('#tr' + rowId).scope().item.stockId = angular.element('#tr' + rowId).scope().item.stockName.stockId;
        angular.element('#tr' + rowId).scope().item.typeId = angular.element('#tr' + rowId).scope().item.stockName.typeId;
        angular.element('#tr' + rowId).scope().item.typeName = angular.element('#tr' + rowId).scope().item.stockName.typeName;
        angular.element('#tr' + rowId).scope().item.spec = angular.element('#tr' + rowId).scope().item.stockName.spec;
        angular.element('#tr' + rowId).scope().item.brand = angular.element('#tr' + rowId).scope().item.stockName.brand;
        angular.element('#tr' + rowId).scope().item.unit = angular.element('#tr' + rowId).scope().item.stockName.unit;
        angular.element('#tr' + rowId).scope().item.stockName = angular.element('#tr' + rowId).scope().item.stockName.stockName;

        $scope.$apply();
    };

    $scope.stockSelect = function (index) {
        $scope.currentRow = '#tr' + index;
        $scope.modalStock.setTitle('库存列表').show();
    };

    $scope.addItem = function(){
        $scope.stockPurchaseDetail = $scope.stockPurchaseDetail || [];
        var last = $scope.stockPurchaseDetail.length > 0 ? $scope.stockPurchaseDetail[$scope.stockPurchaseDetail.length - 1] : {};
        $scope.stockPurchaseDetail.push({
            rowId : $scope.rowId ++,
            id : '',
            listId : '',
            stockId : '',
            stockName : '',
            typeId : '',
            typeName : '',
            spec : '',
            brand : '',
            unit : '',
            num : ''
        });
    };

    $scope.deleteItem = function(row){
        if(row.id && row.id != ''){
            Page.confirm('确认删除当前记录?',function(){
                Page.ajaxPostQuiet('stockPurchaseDetail/delete.do', {id : row.id}, function (resp) {
                    $($scope.stockPurchaseDetail).each(function(idx, _row){
                        if(row.rowId && row.rowId == _row.rowId){
                            $scope.stockPurchaseDetail.splice(idx,1);
                            return false;
                        }
                    });
                });
            });
        }else{
            $($scope.stockPurchaseDetail).each(function(idx, _row){
                if(row.rowId && row.rowId == _row.rowId){
                    $scope.stockPurchaseDetail.splice(idx,1);
                    return false;
                }
            });
        }
    };

    $scope.mouseEnter = function (index, col) {
        rowId = index + 1;
        angular.element('#tr'+rowId).scope()['isShow' + col] = false;
    };

    $scope.mouseLeave = function (index, col) {
        rowId = index + 1;
        angular.element('#tr'+rowId).scope()['isShow' + col] = true;
        angular.element('#tr'+rowId).scope().item.typeName = angular.element('#tr'+rowId).scope().item.typeId.typeName;
    };

}]);